package com.affectu.task;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;
import org.jboss.seam.annotations.Name;

import com.affectu.common.base.EntityBase;
import com.affectu.user.User;

/*
 * 2008-8-15
 * 
 * @author daniel nathan
 */
// @Entity
@Name("task")
@MappedSuperclass()
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Task extends EntityBase implements Comparable<Task> {

	private static final long serialVersionUID = -6782088258477691854L;

	@Field(name = "tag", index = Index.TOKENIZED, analyzer = @Analyzer(impl = ChineseAnalyzer.class))
	private String tag;

	private int budget;

	private int bidDay;

	private int finishDay;

	@Temporal(value = TemporalType.TIMESTAMP)
	private Date publishDate;

	private String attachName;

	private String url;

	private Type type;

	public enum Type {
		INNER, OUTER
	}

	@ManyToOne(cascade = CascadeType.MERGE)
	@JoinColumn(name = "userId", referencedColumnName = "id")
	private User owner;

	@IndexedEmbedded
	@OneToMany(mappedBy = "belongToTask", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List<Bid> allBid;

	@Override
	@DocumentId
	public Long getId() {
		return super.getId();
	}

	@Override
	public void setId(Long id) {
		super.setId(id);
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	@Override
	@Field(store = Store.NO, index = Index.TOKENIZED, analyzer = @Analyzer(impl = ChineseAnalyzer.class))
	@Column(columnDefinition = "MEDIUMTEXT")
	public String getDescription() {
		return super.getDescription();
	}

	@Override
	public void setDescription(String description) {
		super.setDescription(description);
	}

	public int getBudget() {
		return budget;
	}

	public void setBudget(int budget) {
		this.budget = budget;
	}

	public int getBidDay() {
		return bidDay;
	}

	public void setBidDay(int bidDay) {
		this.bidDay = bidDay;
	}

	public int getFinishDay() {
		return finishDay;
	}

	public void setFinishDay(int finishDay) {
		this.finishDay = finishDay;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public List<Bid> getAllBid() {
		return allBid;
	}

	public void setAllBid(List<Bid> allBid) {
		this.allBid = allBid;
	}

	public String getAttachName() {
		return attachName;
	}

	public void setAttachName(String attachName) {
		this.attachName = attachName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public int compareTo(Task annotherTask) {
		if (annotherTask != null && annotherTask.getCreateDate() != null) {
			return annotherTask.getCreateDate().compareTo(this.getCreateDate());
		}
		return 0;
	}
}
